using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Common;
using Controls;
using Microsoft.Win32;
using SExperiment;
using SSettings;

namespace MainView.Windows
{
	public partial class PatientImportWindow : Window, IComponentConnector
	{
		private int LineNum = 1;

		private List<KeyValuePair> columnsHeaderTypes;

		public List<string[]> allData;

		private string[] titiles;

		public List<KeyValuePair> table { get; set; }

		public PatientImportWindow()
		{
			InitializeComponent();
			base.Loaded += PatientImportWindow_Loaded;
		}

		private void PatientImportWindow_Loaded(object sender, RoutedEventArgs e)
		{
			Experiment experiment = base.DataContext as Experiment;
			if (experiment == null)
			{
				return;
			}
			leftPanel.Children.Clear();
			int num = 1;
			foreach (ColumnInfo item in experiment.GridColumns.Columns.Where((ColumnInfo s) => s.Display = (s.Info & 4) == 4).ToList())
			{
				TextBoxEx textBoxEx = new TextBoxEx();
				textBoxEx.Tag = ((item.FieldName == null) ? string.Empty : item.FieldName);
				textBoxEx.Text = item.Header;
				textBoxEx.IsEnabled = false;
				textBoxEx.Width = 160.0;
				textBoxEx.Height = 23.0;
				Thickness margin = (textBoxEx.Margin = new Thickness(0.0, 8.0, 0.0, 8.0));
				leftPanel.Children.Add(textBoxEx);
				ComboBox comboBox = new ComboBox();
				comboBox.Width = 160.0;
				comboBox.Height = 23.0;
				comboBox.Margin = margin;
				comboBox.DisplayMemberPath = "Value";
				comboBox.SelectedValuePath = "Key";
				comboBox.IsEditable = false;
				comboBox.Style = Utility.GetResource("ComboBoxDefault") as Style;
				comboBox.ItemContainerStyle = Utility.GetResource("ComboBoxItemStyleDefault") as Style;
				comboBox.Name = "combox" + num;
				rightPanel.Children.Add(comboBox);
				num++;
			}
		}

		private void combox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			List<TextBoxEx> list = leftPanel.Children.Cast<TextBoxEx>().ToList();
			List<ComboBox> list2 = rightPanel.Children.Cast<ComboBox>().ToList();
			table = new List<KeyValuePair>();
			for (int i = 0; i < list.Count; i++)
			{
				int result = -1;
				int.TryParse(list2[i].SelectedValue.ToString(), out result);
				if (result > -1)
				{
					table.Add(new KeyValuePair
					{
						Key = list[i].Tag,
						Value = list2[i].SelectedValue
					});
				}
			}
			base.DialogResult = true;
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void btnBrowser_Click_1(object sender, RoutedEventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = string.Format("{0}{1}|*.{2}", "csv", Utility.GetMessage("File"), "csv");
			openFileDialog.FileName = string.Empty;
			openFileDialog.FilterIndex = 1;
			openFileDialog.InitialDirectory = Utility.GetMyDocumentFolder();
			openFileDialog.DefaultExt = Experiment.FileTemplateExt;
			if (openFileDialog.ShowDialog() != true)
			{
				return;
			}
			file_name.Text = openFileDialog.FileName;
			List<string[]> list = ReadCSV(openFileDialog.FileName);
			if (list.Count() > LineNum - 1)
			{
				titiles = list[LineNum - 1];
			}
			if (titiles == null || titiles.Count() <= 0)
			{
				return;
			}
			int num = 0;
			columnsHeaderTypes = new List<KeyValuePair>();
			columnsHeaderTypes.Add(new KeyValuePair
			{
				Key = -1,
				Value = Utility.GetResource("msg_please_select_content")
			});
			string[] array = titiles;
			foreach (string value in array)
			{
				KeyValuePair keyValuePair = new KeyValuePair();
				keyValuePair.Key = num;
				keyValuePair.Value = value;
				KeyValuePair item = keyValuePair;
				columnsHeaderTypes.Add(item);
				num++;
			}
			List<ComboBox> list2 = rightPanel.Children.Cast<ComboBox>().ToList();
			List<TextBoxEx> list3 = leftPanel.Children.Cast<TextBoxEx>().ToList();
			for (int j = 0; j < list2.Count; j++)
			{
				ComboBox comboBox = list2[j];
				TextBoxEx textBoxEx = list3[j];
				comboBox.ItemsSource = columnsHeaderTypes;
				comboBox.SelectedIndex = 0;
				foreach (KeyValuePair columnsHeaderType in columnsHeaderTypes)
				{
					if (columnsHeaderType.Value.ToString().Equals(textBoxEx.Text))
					{
						int result = 0;
						int.TryParse(columnsHeaderType.Key.ToString(), out result);
						comboBox.SelectedIndex = result + 1;
					}
				}
			}
			allData = new List<string[]>();
			for (num = LineNum; num < list.Count; num++)
			{
				allData.Add(list[num]);
			}
		}

		public List<string[]> ReadCSV(string filePathName)
		{
			List<string[]> list = new List<string[]>();
			if (Utility.IsFileInUse(filePathName))
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_file_in_use"), Path.GetFileNameWithoutExtension(filePathName)), Utility.GetMessage("Warning"));
				return list;
			}
			StreamReader streamReader = new StreamReader(filePathName, Utility.GetFileEncoding(filePathName));
			string text = "";
			while (text != null)
			{
				text = streamReader.ReadLine();
				if (text != null && text.Length > 0)
				{
					list.Add(text.Split(','));
				}
			}
			streamReader.Close();
			return list;
		}
	}
}
